/**
 * @param {number[][]} grid
 * @param {number} k
 * @return {number[][]}
 */
 var rotateGrid = function(grid, k) {
    const m = grid.length, n = grid[0].length
    const ret = new Array(m).fill(0).map(() => new Array(n).fill(0))
    const deepClone = (t) => {
        let arr = []
        for(let x of t){
            let [...temp] = x
            arr.push(temp)
        }
        return arr
    }
    for(let x = 0; x<k; ++x){
        for(let i = 0; i<m-1; ++i){
            for(let j = i; j<n-i-1; ++j){
                ret[i][j] = grid[i][j+1]
                ret[m-i-1][j+1] = grid[m-i-1][j]
                // [ret[i][j],ret[j+1][i],ret[m-i-1][j+1],ret[j][n-i-1]] =
                // [grid[i][j+1],grid[j][i],grid[m-i-1][j],grid[j+1][n-i-1]]
            }
            
        }
        for(let i = 0; i<n>>1;++i){
            for(let j = i; j<m-i-1; ++j){
                ret[j+1][i] = grid[j][i]
                ret[j][n-i-1] = grid[j+1][n-i-1]
            }
        }
        grid = deepClone(ret)
    }
    return ret
};